<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>结算功能测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ccc; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        button { margin: 5px; padding: 10px; }
        .product-item { border: 1px solid #ddd; margin: 10px 0; padding: 10px; }
        .test-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
        .test-btn { padding: 8px 12px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
        .test-btn:hover { background: #0056b3; }
    </style>
</head>
<body>
    <h1>结算功能测试</h1>
    
    <div class="test-section">
        <h3>1. 模拟购物车数据</h3>
        <button onclick="simulateCartData()">模拟购物车数据</button>
        <button onclick="clearCartData()">清空购物车数据</button>
        <div id="cartStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 测试结算功能</h3>
        <div class="test-buttons">
            <button class="test-btn" onclick="testCheckout()">测试结算页面</button>
            <button class="test-btn" onclick="testDirectPurchase()">测试直接购买</button>
            <button class="test-btn" onclick="checkLocalStorage()">检查localStorage</button>
        </div>
    </div>
    
    <div class="test-section">
        <h3>3. 当前购物车数据</h3>
        <div id="cartData"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 控制台日志</h3>
        <div id="consoleLog"></div>
    </div>

    <!-- 引入必要的脚本 -->
    <script src="js/api.js"></script>
    <script src="js/auth.js"></script>
    <script src="js/cart.js"></script>
    
    <script>
        // 重写console.log来显示在页面上
        const originalLog = console.log;
        const originalWarn = console.warn;
        const originalError = console.error;
        
        function addToConsole(message, type = 'info') {
            const logDiv = document.getElementById('consoleLog');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `<div class="${type}">[${timestamp}] ${message}</div>`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        console.log = function(...args) {
            originalLog.apply(console, args);
            addToConsole(args.join(' '), 'info');
        };
        
        console.warn = function(...args) {
            originalWarn.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        console.error = function(...args) {
            originalError.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        // 模拟购物车数据
        function simulateCartData() {
            const mockCartItems = [
                {
                    product_id: 'F001',
                    product_name: '东北大米',
                    price: 115.3,
                    quantity: 2,
                    selected: true,
                    image: 'https://via.placeholder.com/300x200?text=东北大米',
                    category: 'Food'
                },
                {
                    product_id: 'F007',
                    product_name: '广东腊肠',
                    price: 22.87,
                    quantity: 1,
                    selected: true,
                    image: 'https://via.placeholder.com/300x200?text=广东腊肠',
                    category: 'Food'
                }
            ];
            
            // 保存到localStorage
            localStorage.setItem('checkoutItems', JSON.stringify(mockCartItems));
            
            const statusDiv = document.getElementById('cartStatus');
            statusDiv.innerHTML = '<div class="success">✓ 购物车数据已模拟</div>';
            
            console.log('模拟购物车数据:', mockCartItems);
            updateCartDisplay();
        }
        
        // 清空购物车数据
        function clearCartData() {
            localStorage.removeItem('checkoutItems');
            const statusDiv = document.getElementById('cartStatus');
            statusDiv.innerHTML = '<div class="info">购物车数据已清空</div>';
            updateCartDisplay();
        }
        
        // 测试结算页面
        function testCheckout() {
            console.log('跳转到结算页面...');
            window.open('checkout.html', '_blank');
        }
        
        // 测试直接购买
        function testDirectPurchase() {
            console.log('测试直接购买...');
            window.open('checkout.html?productId=F001&quantity=1', '_blank');
        }
        
        // 检查localStorage
        function checkLocalStorage() {
            const checkoutItems = localStorage.getItem('checkoutItems');
            console.log('localStorage中的checkoutItems:', checkoutItems);
            
            if (checkoutItems) {
                try {
                    const items = JSON.parse(checkoutItems);
                    console.log('解析后的商品数据:', items);
                } catch (e) {
                    console.error('解析localStorage数据失败:', e);
                }
            } else {
                console.log('localStorage中没有checkoutItems数据');
            }
        }
        
        // 更新购物车显示
        function updateCartDisplay() {
            const cartDataDiv = document.getElementById('cartData');
            const checkoutItems = localStorage.getItem('checkoutItems');
            
            if (checkoutItems) {
                try {
                    const items = JSON.parse(checkoutItems);
                    cartDataDiv.innerHTML = `
                        <h4>购物车商品 (${items.length}件):</h4>
                        ${items.map(item => `
                            <div class="product-item">
                                <h4>${item.product_name}</h4>
                                <p>价格: ¥${item.price} × ${item.quantity} = ¥${(item.price * item.quantity).toFixed(2)}</p>
                                <p>分类: ${item.category}</p>
                            </div>
                        `).join('')}
                    `;
                } catch (e) {
                    cartDataDiv.innerHTML = '<div class="error">解析购物车数据失败</div>';
                }
            } else {
                cartDataDiv.innerHTML = '<div class="info">购物车为空</div>';
            }
        }
        
        // 页面加载完成后运行测试
        window.addEventListener('load', () => {
            console.log('页面加载完成，结算功能测试准备就绪');
            updateCartDisplay();
            
            // 检查模块加载状态
            setTimeout(() => {
                console.log('模块加载状态:');
                console.log('- API:', !!window.api);
                console.log('- AuthManager:', !!window.authManager);
                console.log('- CartManager:', !!window.cartManager);
            }, 100);
        });
    </script>
</body>
</html>
